package net.mmoserver;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Klasa odpowiedzialna za poruszanie oraz wcisniete klawisze przez graczy
 * @author Daniel Schmidt
 *         
 */

public class MovementSocket {
	  // Zmienne klasy
	  private DatagramSocket socket;
	  private DatagramPacket sendPacket;
	  @SuppressWarnings("unused")
	  private int port;
	  byte[] bufor = new byte[1024];
	  private DatagramPacket readPacket = new DatagramPacket(bufor,bufor.length);
	  
public MovementSocket(int port) throws SocketException{
	 this.port = port;
	 socket = new DatagramSocket(port);
}
 /**
  * Glowna metoda klasy
  * @author Daniel Schmidt
  *
  */
  public void Run(boolean run){
	
	  while(run){  
		  try {
			getMessage();
		} catch (IOException e) {
			e.printStackTrace();
			// KOMUNIKAT W GUI
		}
	  }
                   }
  
  /**
   * Wyslanie wiadomosci
   * @author Daniel Schmidt
   *
   */
  @SuppressWarnings("unused")
  private void send(DatagramPacket from, String outMessage) throws IOException{
	      bufor = (outMessage).getBytes();
	      sendPacket = new DatagramPacket(bufor,bufor.length,from.getAddress(),from.getPort());
	      socket.send(sendPacket);
       }
  /**
   * Odbior wiadomosci oraz wyslanie odpowiedzi
   * @author Daniel Schmidt
   * 
   */
  private void getMessage() throws IOException{
	  socket.receive(readPacket);
  	  String rcvd = new String(readPacket.getData(), 0, readPacket.getLength());
  	  
  	     switch(Integer.parseInt(rcvd)){
  	     // Uzupelnic przypadki!!!
  	     // Trzeba sprawdzic pozycje gracza
  	   
  	     }
  }
}
